<?php

namespace app\index\controller;

use think\Cache;
use think\Controller;
use think\Db;
use think\Exception;
use think\Loader;
use think\Request;
use think\Session;
use think\Validate;
use tree\Tree;

class Index extends Controller
{
    /**
     * 生成省市联动数据文件
     * */
    public function createRegionFile()
    {
        $region_list = Db::name('province')->field('province_id id,name')->order('province_id asc')->select();
        if ($region_list) {
            foreach ($region_list as &$v) {
                // 将名称当中的省和市替换为空
                $v['name'] = str_replace(['省', '市'], '', $v['name']);

                $city_list = Db::name('city')->field('city_id id,name,province_id pid')->where(['province_id' => $v['id']])->order('city_id asc')->select();
                if ($city_list) {
                    foreach ($city_list as &$val) {
                        $val['name'] = str_replace(['省', '市'], '', $val['name']);

                        $area_list = Db::name('area')->field('area_id id,name,city_id pid')->where(['city_id' => $val['id']])->order('area_id asc')->select();
                        $val['area_list'] = $area_list ? : [];
                    }
                }
                $v['city_list'] = $city_list ? : [];
            }
        }

        file_put_contents(RUNTIME_PATH . 'region.json', json_encode($region_list, JSON_UNESCAPED_UNICODE));
    }

    /**
     * 获取车辆列表
     * */
    public function getCarList()
    {
        $input = Request()->param();

        $user_id = $input['user_id'];

        if (empty($user_id)) apiReturn(2, '参数错误');

        $list = Db::name('car')->whereIn('team_user_id', [0, $user_id])->select();

        if ($list) apiReturn(1, '成功', $list);
    }
}
